package whf.shop.service;

import java.util.List;

import whf.framework.exception.FindException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.exception.UpdateException;
import whf.framework.util.BeanFactory;
import whf.shop.dao.CurrencyDAO;
import whf.shop.entity.Currency;

/**
 * 服务对象具体实现，委托相应的dao对象实现相关操作
 * 
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2007-01-11
 */
public class CurrencyServiceImp extends whf.framework.service.ServiceImp<Currency>
		implements CurrencyService {

	private CurrencyDAO currencyDAO;

	public void setCurrencyDAO(CurrencyDAO currencyDAO) {
		this.currencyDAO = currencyDAO;
	}

	public static CurrencyService getCurrencyService()
			throws ServiceNotFoundException {
		return (CurrencyService) BeanFactory
				.getService(CurrencyServiceImp.class);
	}

	/* (non-Javadoc)
	 * @see whf.shop.service.CurrencyService#findBaseCurrency()
	 */
	public Currency findBaseCurrency() throws FindException {
		List<Currency> list = this.currencyDAO.find("t.base=?", Boolean.TRUE);
		if(list.size() > 0){
			return list.get(0);
		} else {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see whf.shop.service.CurrencyService#findNonBaseCurrency()
	 */
	public List<Currency> findNonBaseCurrency() throws FindException {
		List<Currency> list = this.currencyDAO.find("t.base = ?", Boolean.FALSE);
		return list;
	}

	/* (non-Javadoc)
	 * @see whf.shop.service.CurrencyService#findEffectiveCurrencies()
	 */
	public List<Currency> findEffectiveCurrencies() throws FindException {
		List<Currency> list = this.currencyDAO.find("t.effective=?", Boolean.TRUE);
		return list;
	}

	/* (non-Javadoc)
	 * @see whf.shop.service.CurrencyService#changeBaseCurrency(whf.shop.entity.Currency, whf.shop.entity.Currency)
	 */
	public void changeBaseCurrency(Currency newBaseCurrency) throws FindException, UpdateException {
		if(newBaseCurrency.isBase() || newBaseCurrency == null){
			return;
		}else{
			double oldExchangeRate = 1;
			Currency oldBaseCurrency = this.findBaseCurrency();
			if(oldBaseCurrency != null){
				//update all exchange rate
				List l = this.currencyDAO.find("t.id!=? and t.id !=?", new Object[]{newBaseCurrency.getId(), oldBaseCurrency.getId()});
				for(int i=0;i<l.size();i++){
					Currency cur = (Currency)l.get(i);
					if(cur.getId() == oldBaseCurrency.getId() || 
							cur.getId() == newBaseCurrency.getId() || 
							cur.getExchangeRate() == 0){
						continue;
					}
					cur.setExchangeRate(oldBaseCurrency.getExchangeRate() / cur.getExchangeRate() * newBaseCurrency.getExchangeRate());
					this.currencyDAO.update(cur);
				}
				//update old exchange rate
				oldExchangeRate = oldBaseCurrency.getExchangeRate();
				oldBaseCurrency.setBase(false);
				oldBaseCurrency.setExchangeRate(newBaseCurrency.getExchangeRate());
				this.currencyDAO.update(oldBaseCurrency);
			}
			//update new exchange rate
			newBaseCurrency.setBase(true);
			newBaseCurrency.setExchangeRate(oldExchangeRate);
			this.currencyDAO.update(newBaseCurrency);
		}
	}

}